﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public class EmployeeAllowance : NameIDPair
    {
        private string _name;
        private string _allowance;
        private Int64 _id;

        new public string Name
        {
            get { return _name; }
        }
        public string Allowance
        {
            get { return _allowance; }
        }
        new public Int64 ID
        {
            get { return _id; }
        }
        
        public EmployeeAllowance(string name, string allowance, Int64 id)
        {
            _name = name;
            _allowance = allowance;
            _id = id;
        }

        public NameIDPair ToNameIDPair()
        {
            return new NameIDPair(Name, ID);
        }

        /// <summary>
        /// retrieves a list of emps_from_database' names and their used/GetTotal _holiday Allowance to show in a datagridview
        /// </summary>
        /// <param Name="GetEmployeeIDs">_employee'pShift GetEmployeeIDs from the SQLite CurrentFile</param>
        /// <param Name="year">year in question (_holiday Allowance calculated per year)</param>
        /// <returns>a list of</returns>
        public static List<EmployeeAllowance> GetDataSource(Int64[] IDs, int year)
        {
            var format = Settings.Default.FORMAT_HolidayAllowance;
            var result = new List<EmployeeAllowance>();
            foreach (int id in IDs)
            {
                var name = SQLiteAccess.GetEmployeeFullName(id);
                
                var used = SQLiteAccess.GetUsedHoliday(id, year);                
                var allowances = SQLiteAccess.GetHolidayAllowances(id, year);
                var builder = new StringBuilder();
                var bits = format.Split(';');
                foreach (var bit in bits)
                {
                    builder.Append(
                        getBit(bit, used, allowances));

                }                
                var another = new EmployeeAllowance(
                    name,
                    builder.ToString(),
                    id);
                result.Add(another);                
            }
            return result;
        }

        private static string getBit(
            string pKeyword,
            int pUsedHolidays,
            KeyValuePair<int, bool> pAllowance)
        {
            switch (pKeyword.ToLower())
            {
                case "uh":
                    {
                        return pUsedHolidays.ToString();
                    }
                case "ha":
                    {
                        return String.Concat(
                            pAllowance.Key.ToString(),
                            pAllowance.Value ? Resources.CALCULATED_VALUE_INDICATOR : String.Empty);
                    }
                case "per":
                    {                        
                        if (pAllowance.Key == 0)
                        {
                            return "0%";
                        }
                        return ((100 * pUsedHolidays) / pAllowance.Key).ToString() +
                            "%";
                    }
                default:
                    {
                        return pKeyword; // literal
                    }
            }            
        }
        
    }
}
